//
//  ScreenShotViewController.m
//  ObjectiveCTools
//
//  Created by HPCL20190110 on 2019/3/13.
//  Copyright © 2019 ChinaRapidFinance. All rights reserved.
//

#import "ScreenShotViewController.h"

@interface ScreenShotViewController ()

@property (weak, nonatomic) IBOutlet UIView *viewSample;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation ScreenShotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

/// 方案一
- (IBAction)actionOne:(id)sender {
    
    //截屏
    UIView *view = [[UIApplication sharedApplication].keyWindow snapshotViewAfterScreenUpdates:YES];

    NSLog(@"%@", view);
}

/// 方案二 这个也可以截取整个屏幕,设置好
- (IBAction)actionTwo:(id)sender {
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height), NO, 0.0);
    
    /// 如果是指定view 替换 [UIApplication sharedApplication].keyWindow 就可以
    [[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
   
    UIGraphicsEndImageContext();
    
    //保存
    UIImageWriteToSavedPhotosAlbum(screenShotImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}


/// 方案三
- (IBAction)actionThree:(id)sender {
    //iOS 13 bug,原来的方式失效了,截图要先移除,然后渲染截图,然后再添加上来
    //https://www.jianshu.com/p/0ab36955d03a
    [self.scrollView removeFromSuperview];
    UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, NO, UIScreen.mainScreen.scale);
    CGPoint savedContentOffset = self.scrollView.contentOffset;
    CGRect savedFrame = self.scrollView.frame;
    self.scrollView.contentOffset = CGPointZero;
    self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
    [self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.view addSubview:self.scrollView];
    self.scrollView.contentOffset = savedContentOffset;
    self.scrollView.frame = savedFrame;
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}


///方案三, 如果UIScrollView 是用 约束布局的,截图的时候,使用下面这段代码,可正常截图,不会闪动
//- (UIImage *)screenShoot {
//    self.bottomView.hidden = YES;
//    [self.scrollView removeFromSuperview];
//    UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, NO, UIScreen.mainScreen.scale);
//    CGPoint savedContentOffset = self.scrollView.contentOffset;
//    CGRect savedFrame = self.scrollView.frame;
//    self.scrollView.contentOffset = CGPointZero;
//    self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
//    [self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    [self.view addSubview:self.scrollView];
//    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.view);
//        make.top.equalTo(self.baseNavigationBar.mas_bottom);
//        make.centerX.equalTo(self.view);
//        make.bottom.equalTo(self.view);
//    }];
//    self.bottomView.hidden = NO;
//    self.scrollView.frame = savedFrame;
//    self.scrollView.contentOffset = savedContentOffset;
//    return image;
//}



/// 保存
- (void)saveToAlbum:(UIView *)view {
    
    // 开启一个位图上下文
    UIGraphicsBeginImageContext(view.bounds.size);
    //把画板内容渲染到上下文当中
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    //从上下文当中取出图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    //把图片保存到相册当中  这个方法是固定的,不能乱写
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error == nil) {
        NSLog(@"保存成功");
    }
}

@end
